package com.zeny.xdclass.controller;

import com.zeny.xdclass.common.ResponseResult;
import com.zeny.xdclass.dto.VideoOrderDto;
import com.zeny.xdclass.service.VideoOrderService;
import com.zeny.xdclass.utils.IpUtils;
import com.zeny.xdclass.utils.QRCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName OrderController
 * @Description TODO
 * @Author zeny
 */

@RestController
@RequestMapping("/user/api/v1/order")
@Api(tags = "订单模块")
public class OrderController {

    @Autowired
    private VideoOrderService videoOrderService;
    @Autowired
    private QRCodeUtils qrCodeUtils;

    @ApiOperation(value = "下单")
    @ApiImplicitParam(name = "videoId", required = true)
    @GetMapping("/add")
    public void saveOrder(@RequestParam(value = "videoId", required = true) Integer videoId,
                          HttpServletRequest request, HttpServletResponse response) throws Exception {
        String ip = IpUtils.getIpAddr(request);
        int userId = 3;
        VideoOrderDto videoOrderDto = new VideoOrderDto();
        videoOrderDto.setUserId(userId);
        videoOrderDto.setIp(ip);
        videoOrderDto.setVideoId(videoId);
        String codeUrl = videoOrderService.save(videoOrderDto);
        if (StringUtils.isEmpty(codeUrl)) {
           return;
        }
        //返回二维码给用户
        qrCodeUtils.createQRCode(codeUrl, response.getOutputStream());
    }

}
